package cn.echo.modules.sys.controller.articlecontroller;

import cn.echo.modules.sys.entity.Article;
import cn.echo.modules.sys.entity.User;
import cn.echo.modules.sys.service.ArticleService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2017/4/15.
 */
@Controller
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    ArticleService articleService;
    @RequestMapping(value = "add",method = RequestMethod.POST)
    public String add(Article article){
        article.setId(UUID.randomUUID().toString());
        article.setLogo("img/logo.png");
        User user = new User();
        user.setId("001");
        article.setCreateDate(new Date());
        article.setUpdateDate(new Date());
        articleService.add(article);
        return "redirect:findAll?pageNum=1&pageSize=8";
    }

    @RequestMapping(value = {"findAll",""},method = RequestMethod.GET)
    public String findAll(Article article,int pageNum,int pageSize, Model model){
        //赋值页面页码大小自动帮我们计算，我们只需要在页面上使用PageInfo的数据就行
        PageHelper.startPage(pageNum, pageSize);
        Page<Article> articleList = (Page) articleService.findAll(new Article());
        //分页信息
        PageInfo<Article> pageInfo = new PageInfo(articleList);
        model.addAttribute("page",pageInfo);
        return "sys/article/showArticle";
    }


    @RequestMapping(value = "delete",method = RequestMethod.GET)
    public String delete(String id){
        Article article = new Article();
        article.setId(id);
        articleService.delete(article);
        return "redirect:findAll?pageNum=1&pageSize=8";
    }

    @RequestMapping(value = "findByIdToUpdate")
    public String findByIdToUpdate(String id,Model model){
        Article article = articleService.findById(id);
        model.addAttribute("article",article);
        return "sys/article/updateArticle";
    }

    @RequestMapping(value="update",method = RequestMethod.POST)
    public String update(Article article){
        article.setUpdateDate(new Date());
        articleService.update(article);
        return "redirect:findAll?pageNum=1&pageSize=8";
    }
    @RequestMapping(value="Serach",method = RequestMethod.GET)
    public String serach(String title, Model model, @RequestParam(defaultValue = "1",required = false) int pageNum,
                         @RequestParam(defaultValue = "10",required = false) int pageSize){
        ArticleService articleService = new ArticleService();
        //赋值页面页码大小自动帮我们计算，我们只需要在页面上使用PageInfo的数据就行
        PageHelper.startPage(pageNum,pageSize);
        //分页信息
        Page<Article> articleList = (Page) articleService.serachArticle(title);
        if(articleList.size()<0){
            model.addAttribute("message","没有搜索符合的条件");
            return "sys/article/showArticle";
        }else{
            PageInfo<Article> pageInfo = new PageInfo(articleList);
            model.addAttribute("page",pageInfo);
            return "sys/article/showArticle";
        }
    }

    @RequestMapping(value = "findByUserId",method = RequestMethod.GET)
    public String findByUserId(String id,Model model){
        Article article = articleService.findById(id);
       model.addAttribute("article",article);
        return "sys/article/updateArticle";
    }

    @RequestMapping(value = "returnShow",method = RequestMethod.GET)
    public String returnShow(){
        return "sys/article/addArticle";
    }

    @RequestMapping(value = "addArticle",method = RequestMethod.GET)
    public String addArticle(){
        return "sys/article/addArticle";
    }

}
